package com.hbnu.net;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class TcpClientDemo2 {
    public static void main(String[] args) {
        try {
            //创建Socket，并指定服务端ip和端口
            Socket socket = new Socket("127.0.0.1",8888);

            //获取流资源
            OutputStream out = socket.getOutputStream();

            InputStream in = socket.getInputStream();

            //准备发送的数据
            String content = "土豆土豆，我是地瓜，收到请回复，OVER";
            out.write(content.getBytes());

            //接收服务端返回的数据
            byte[] bytes = new byte[1024];
            int length = in.read(bytes);
            System.out.println(new String(bytes,0,length));

            //关闭资源
            in.close();
            out.close();
            socket.close();

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
